package ecom.beans.dao;

import ecom.beans.ProductstoreBean;
import ecom.beans.exception.AccountBeanException;
import ecom.beans.exception.DAONotUniqueException;
import ecom.beans.exception.ProductstoreBeanException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class ProductstoreDAO extends DAO {

    public static Long saveProductstore(ProductstoreBean productstore) throws ProductstoreBeanException, AccountBeanException, DAONotUniqueException {
        String exception = "";
        boolean isException = false;

        if (AccountDAO.getByIBAN(productstore.getIban().getIban()) == null) {
            if (productstore.getIban().getIban().length() > 34 || productstore.getIban().getIban().length() < 34) {
                throw new AccountBeanException("IBAN must contain 34 characters");
            }
        } else {
            throw new DAONotUniqueException("This account already exist");
        }
        if (productstore.getName().length() > 50) {
            isException = true;
            exception += exception.equals("") ? "The name of the productstore should not exceed 50 characters"
                    : " ,the name of the productstore should not exceed 50 characters";
        }
        if (productstore.getAdress().length() > 100) {
            isException = true;
            exception += exception.equals("") ? "The adress of the productstore should not exceed 100 characters"
                    : " ,the adress of the productstore should not exceed 100 characters";
        }
        if (productstore.getPostalCode().length() > 10) {
            isException = true;
            exception += exception.equals("") ? "The name of the productstore should not exceed 10 characters"
                    : " ,the name of the productstore should not exceed 10 characters";
        }
        if (productstore.getCity().length() > 50) {
            isException = true;
            exception += exception.equals("") ? "The city of the productstore should not exceed 50 characters"
                    : " ,the city of the productstore should not exceed 50 characters";
        }
        if (productstore.getTel().length() > 20) {
            isException = true;
            exception += exception.equals("") ? "The phone number of the productstore should not exceed 20 characters"
                    : " ,the phone number of the productstore should not exceed 20 characters";
        }
        if (isException) {
            throw new ProductstoreBeanException(exception);
        }

        saveOrUpdate(productstore);
        return productstore.getIdProductStore();
    }

    public static void deleteProductstore(ProductstoreBean productstore) {
        delete(productstore);
    }

    public static void updateProductstore(ProductstoreBean productstore) {
        saveOrUpdate(productstore);
    }

    public static Set<ProductstoreBean> getAllProductstores(String order) {
        return fetchAll("ProductstoreBean.findAll", null, order);
    }

    public static ProductstoreBean getByIdProductstore(Long idProductStore) {
        List parameters = new ArrayList();
        parameters.add("idProductStore");
        parameters.add(idProductStore);
        return (ProductstoreBean) fetchOne("ProductstoreBean.findByIdProductStore", parameters);
    }
}
